import 'dart:convert';

import 'RechargeBanner.dart';
import 'RoomCategoryRankInfoList.dart';
import 'room_info.dart';
import 'user_info.dart';
import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart' as encrypt;

class RoomDataModel {
  RoomDataModel({
      this.roomInfo, 
      this.userInfo, 
      this.roomGameList, 
      this.showCharm, 
      this.hatPermit, 
      this.hatDeadlineTime, 
      this.hatDeadlineTimeText, 
      this.deadlinePermit, 
      this.deadlineTime, 
      this.deadlineTimeText, 
      this.weddingInfo, 
      this.roomPkVo, 
      this.userDrivingInfo, 
      this.followDrivingInfo, 
      this.topDrivings, 
      this.showDriving, 
      this.drivingCenterUrl, 
      this.followUserFlag, 
      this.roomTokenBean, 
      this.drawGameRoom, 
      this.isTodayBirthday, 
      this.roomExtInfo, 
      this.maskedType, 
      this.maskTotalTime, 
      this.maskRemainTime, 
      this.roomInfoVO, 
      this.showSweetBloomEntry, 
      this.roomNewUserWelfareVO, 
      this.backNewUserWelfareVO, 
      this.roomCategoryRankInfoList, 
      this.hourRankTime, 
      this.welcomeBanner, 
      this.rechargeBanner, 
      this.starPoolV2Vo, 
      this.noticeBubbleMsg,});

  RoomDataModel.fromJson(dynamic json) {
    roomInfo = json['roomInfo'] != null ? RoomInfo.fromJson(json['roomInfo']) : null;
    userInfo = json['userInfo'] != null ? UserInfo.fromJson(json['userInfo']) : null;
    roomGameList = json['roomGameList'];
    showCharm = json['showCharm'];
    hatPermit = json['hatPermit'];
    hatDeadlineTime = json['hatDeadlineTime'];
    hatDeadlineTimeText = json['hatDeadlineTimeText'];
    deadlinePermit = json['deadlinePermit'];
    deadlineTime = json['deadlineTime'];
    deadlineTimeText = json['deadlineTimeText'];
    weddingInfo = json['weddingInfo'];
    roomPkVo = json['roomPkVo'];
    userDrivingInfo = json['userDrivingInfo'];
    followDrivingInfo = json['followDrivingInfo'];
    topDrivings = json['topDrivings'];
    showDriving = json['showDriving'];
    drivingCenterUrl = json['drivingCenterUrl'];
    followUserFlag = json['followUserFlag'];
    if (json['roomTokenBean'] != null) {
      // 获取当前时间戳（秒）
      int time = DateTime.now().millisecondsSinceEpoch ~/ 1000;
      tokenMap = decryptRoomEnterCheckData(json['roomTokenBean'], time.toString());
      print("roomTokenBean >>>>>>  ${tokenMap.toString()}");
    }
    roomTokenBean = json['roomTokenBean'];
    drawGameRoom = json['drawGameRoom'];
    isTodayBirthday = json['isTodayBirthday'];
    roomExtInfo = json['roomExtInfo'];
    maskedType = json['maskedType'];
    maskTotalTime = json['maskTotalTime'];
    maskRemainTime = json['maskRemainTime'];
    roomInfoVO = json['roomInfoVO'];
    showSweetBloomEntry = json['showSweetBloomEntry'];
    roomNewUserWelfareVO = json['roomNewUserWelfareVO'];
    backNewUserWelfareVO = json['backNewUserWelfareVO'];
    if (json['roomCategoryRankInfoList'] != null) {
      roomCategoryRankInfoList = [];
    }
    hourRankTime = json['hourRankTime'];
    welcomeBanner = json['welcomeBanner'];
    rechargeBanner = json['rechargeBanner'] != null ? RechargeBanner.fromJson(json['rechargeBanner']) : null;
    starPoolV2Vo = json['starPoolV2Vo'];
    noticeBubbleMsg = json['noticeBubbleMsg'];
  }
  RoomInfo? roomInfo;
  UserInfo? userInfo;
  dynamic? roomGameList;
  dynamic? showCharm;
  dynamic? hatPermit;
  dynamic? hatDeadlineTime;
  dynamic? hatDeadlineTimeText;
  dynamic? deadlinePermit;
  dynamic? deadlineTime;
  dynamic? deadlineTimeText;
  dynamic? weddingInfo;
  dynamic? roomPkVo;
  dynamic? userDrivingInfo;
  dynamic? followDrivingInfo;
  dynamic? topDrivings;
  dynamic? showDriving;
  String? drivingCenterUrl;
  dynamic? followUserFlag;
  String? roomTokenBean;

  Map<String, dynamic>? tokenMap;
  dynamic? drawGameRoom;
  bool? isTodayBirthday;
  String? roomExtInfo;
  dynamic? maskedType;
  dynamic? maskTotalTime;
  dynamic? maskRemainTime;
  dynamic? roomInfoVO;
  bool? showSweetBloomEntry;
  dynamic? roomNewUserWelfareVO;
  dynamic? backNewUserWelfareVO;
  List<RoomCategoryRankInfoList>? roomCategoryRankInfoList;
  int? hourRankTime;
  dynamic? welcomeBanner;
  RechargeBanner? rechargeBanner;
  dynamic? starPoolV2Vo;
  String? noticeBubbleMsg;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    if (roomInfo != null) {
      map['roomInfo'] = roomInfo?.toJson();
    }
    if (userInfo != null) {
      map['userInfo'] = userInfo?.toJson();
    }
    map['roomGameList'] = roomGameList;
    map['showCharm'] = showCharm;
    map['hatPermit'] = hatPermit;
    map['hatDeadlineTime'] = hatDeadlineTime;
    map['hatDeadlineTimeText'] = hatDeadlineTimeText;
    map['deadlinePermit'] = deadlinePermit;
    map['deadlineTime'] = deadlineTime;
    map['deadlineTimeText'] = deadlineTimeText;
    map['weddingInfo'] = weddingInfo;
    map['roomPkVo'] = roomPkVo;
    map['userDrivingInfo'] = userDrivingInfo;
    map['followDrivingInfo'] = followDrivingInfo;
    map['topDrivings'] = topDrivings;
    map['showDriving'] = showDriving;
    map['drivingCenterUrl'] = drivingCenterUrl;
    map['followUserFlag'] = followUserFlag;
    map['roomTokenBean'] = roomTokenBean;
    map['drawGameRoom'] = drawGameRoom;
    map['isTodayBirthday'] = isTodayBirthday;
    map['roomExtInfo'] = roomExtInfo;
    map['maskedType'] = maskedType;
    map['maskTotalTime'] = maskTotalTime;
    map['maskRemainTime'] = maskRemainTime;
    map['roomInfoVO'] = roomInfoVO;
    map['showSweetBloomEntry'] = showSweetBloomEntry;
    map['roomNewUserWelfareVO'] = roomNewUserWelfareVO;
    map['backNewUserWelfareVO'] = backNewUserWelfareVO;
    if (roomCategoryRankInfoList != null) {
    }
    map['hourRankTime'] = hourRankTime;
    map['welcomeBanner'] = welcomeBanner;
    if (rechargeBanner != null) {
      map['rechargeBanner'] = rechargeBanner?.toJson();
    }
    map['starPoolV2Vo'] = starPoolV2Vo;
    map['noticeBubbleMsg'] = noticeBubbleMsg;
    return map;
  }

  Map<String, dynamic> decryptRoomEnterCheckData(String data, String timeStr) {
    String keyStr = '';
    if (timeStr.length >= 8) {
      keyStr += timeStr[0];
      keyStr += timeStr[2];
      keyStr += timeStr[5];
      keyStr += timeStr[7];
    }

    // 计算 MD5 哈希值
    var bytes = utf8.encode(keyStr);
    var digest = md5.convert(bytes);
    String md5Str = digest.toString();
    String aesKeyStr = md5Str.substring(0, 16);

    // 创建 AES 解密器
    final key = encrypt.Key.fromUtf8(aesKeyStr); // 使用别名调用 Key
    final iv = encrypt.IV.fromLength(16); // 使用别名调用 IV
    final encrypter = encrypt.Encrypter(
        encrypt.AES(key, mode: encrypt.AESMode.ecb)); // 使用别名调用相关类

    // 解密数据
    try {
      final encrypted = encrypt.Encrypted.fromBase64(data); // 使用别名调用 Encrypted
      String jsonStr = encrypter.decrypt(encrypted, iv: iv);

      // 解析 JSON 字符串
      Map<String, dynamic> val = json.decode(jsonStr);
      return val;
    } catch (e) {
      return {};
    }
  }

}